package com.example.geckohurricanes;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ItemListPageActivity extends Activity {

	ItemDatabaseHandler db;
	List<Item> items;
	ArrayList<Item> date, name, lost, found;
	List<String> sName;
	int lof = 2;
	ArrayAdapter<List> adapterGen, adapterName, adapterLost, adapterFound;
	ListView listview;
	String SORTING;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.itemlistpage);
		
		db = new ItemDatabaseHandler(this);
		items = db.getAllItems();
		
		for (Item i : items) {
			if (i.getLof() == 1) {
				lost.add(i);
			} else if (i.getLof() == 2) {
				found.add(i);
			}
		}
		
		
		Button back = (Button) findViewById(R.id.btnback);
		back.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		listview = (ListView) findViewById(R.id.listview);
		
		adapterGen = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
		adapterLost = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lost);
		adapterFound = new ArrayAdapter(this, android.R.layout.simple_list_item_1, found);
	    
		if (SORTING == null) {
			listview.setAdapter(adapterGen);
		} else if (SORTING.equals("found")) {
			listview.setAdapter(adapterFound);
			lof = 1;
		} else if (SORTING.equals("lost")) {
			listview.setAdapter(adapterLost);
			lof = 2;
		}
		
		    
	   Button category = (Button) findViewById(R.id.btncat);
		category.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(lof == 2) {
					Intent a = new Intent(getApplicationContext(), ItemListPageActivity.class);
					a.putExtra("SORTING", "found");
					Toast.makeText(getApplicationContext(), "Found Items", Toast.LENGTH_SHORT).show();
					startActivity(a);
					finish();
				} else if (lof == 1) {
					Intent a = new Intent(getApplicationContext(), ItemListPageActivity.class);
					a.putExtra("SORTING", "lost");
					Toast.makeText(getApplicationContext(), "Lost Items", Toast.LENGTH_SHORT).show();
					startActivity(a);
					finish();
				}
			}
		});
		
		Button name = (Button) findViewById(R.id.btnname);
		name.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent a = new Intent(getApplicationContext(), ItemListPageActivity.class);
				startActivity(a);
				finish();
			}
		});

		
	}
	

} 
